package io.finer.erp.jeecg.jolimark.print;

import io.finer.erp.jeecg.jolimark.enums.PrinterEnum;
import io.finer.erp.jeecg.jolimark.model.PrintRequestModel;
import io.finer.erp.jeecg.jolimark.model.PrintTemplateRequestModel;

/**
 * 云模板打印
 * 
 * @author Jolimark
 *
 */
public class TemplatePrint extends AbstractPrint {

	/**
	 * 打印云模板
	 * 
	 * <p>
	 * 适用场景：用户创建打印模版
	 * </p>
	 * <p>
	 * 适用机型：CLP-180、CLQ-200、CLQ-200F、CFP-535、CFP-536、CFP-820
	 * </p>
	 * 
	 * @param printerEnum 打印机型号
	 * @param templateId  模板ID
	 * @param contentJSON 内容JSON
	 * @param paperHeight 打印纸高度
	 */
	public void print(PrinterEnum printerEnum, String templateId, String contentJSON, int paperHeight) {
		if (printerEnum.getType() == 1) {
			Out("错误：不支持MCP系列打印机");
			return;
		}
		PrintTemplateRequestModel model = buildTemplateRequest(printerEnum, templateId);
		model.setBillContent(contentJSON);
		model.setPaperHeight(paperHeight);
		print("PrintHtmlTemplate", model);
	}

	@Override
	protected String convert(PrintRequestModel model) {
		String baseString = super.convert(model);
		PrintTemplateRequestModel expressModel = (PrintTemplateRequestModel) model;
		StringBuilder result = new StringBuilder();
		result.append(baseString);
		result.append("&template_id=").append(expressModel.getTemplateId());
		return result.toString();
	}

	private PrintTemplateRequestModel buildTemplateRequest(PrinterEnum printerEnum, String templateId) {
		PrintRequestModel baseModel = buildRequest(printerEnum);
		PrintTemplateRequestModel templateRequestModel = new PrintTemplateRequestModel();
		templateRequestModel.setAccessToken(baseModel.getAccessToken());
		templateRequestModel.setAppId(baseModel.getAppId());
		templateRequestModel.setCopies(baseModel.getCopies());
		templateRequestModel.setCusOrderId(baseModel.getCusOrderId());
		templateRequestModel.setDeviceIds(baseModel.getDeviceIds());
		templateRequestModel.setPaperType(baseModel.getPaperType());
		templateRequestModel.setPaperWidth(baseModel.getPaperWidth());
		templateRequestModel.setTex(baseModel.getTex());
		templateRequestModel.setTimeout(baseModel.getTimeout());

		templateRequestModel.setTemplateId(templateId);

		return templateRequestModel;
	}
}
